package com.golang.api.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.service.WxPayService;
import com.golang.api.domain.OrderInfo;
import com.golang.api.mapper.OrderInfoMapper;
import com.golang.api.service.IOrderInfoService;
import com.golang.api.util.IpUtil;
import com.golang.api.util.Result;
import com.golang.api.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-12
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private WxPayService wxPayService;
    @Override
    @Transactional
    public Result prepay(HttpServletRequest request) {
        WxPayMpOrderResult result = null;
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setOutTradeNo("order.getOrderSn()");
            orderRequest.setOpenid("openid");
            orderRequest.setBody("订单：" + "order.getOrderSn()");
            // 元转成分
            int fee = 0;
            BigDecimal actualPrice = BigDecimal.ONE;
            fee = actualPrice.multiply(new BigDecimal(100)).intValue();
            orderRequest.setTotalFee(fee);
            orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));

            result = wxPayService.createOrder(orderRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.error("订单创建失败");
        }

        return ResultGenerator.success(result);
    }
}
